﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Lamex.Library;
using Lamex.Library.CommonClass;
using DevExpress.Utils;
using Lamex.Base;
using Lamex.SIESystemManager.Model;
using Lamex.SIESystemManager.BLL;
using Lamex.Interface;

namespace Lamex.SIESystemManager.UI
{
    public partial class FrmBIEOperationalDefinition : frmBaseDataForm
    {
        public string IsEdit = "";
        public string KeysName = "";
        private string[] strList = new string[12];
        public FrmBIEOperationalDefinition()
        {
            InitializeComponent();
            _SummaryView = new DevGridView(gridView2);
            gridView2.DoubleClick += new EventHandler(OnGridViewDoubleClick); //主表DoubleClict
            frmGridViewCust.RegisterGrid(gridView2);
            DevStyle.SetGridControlLayout(gcView, false);//表格设置   

            DevStyle.SetSummaryGridViewLayout(gridView2);
            DevStyle.SetDetailGridViewLayout(gridView2);
            ShowSummary(); //下载显示数据.
            tpSummary = xtraTabPage1;
            tcBusiness = xtraTabControl1;
            tpSummary.PageVisible = true;
            //tpDetail.PageVisible = false;
            tcBusiness.ShowTabHeader = DefaultBoolean.True;
            tcBusiness.SelectedTabPage = tpSummary;
            txtOperationDefine.CaptionVisible = true;
            txtWorkStation.CaptionVisible = true;
        }
        private void ShowSummary()
        {
            try
            {

                xtraTabPage2.PageEnabled = false;
                xtraTabPage1.PageEnabled = true;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 加载按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmBIEOperationalDefinition_Load(object sender, EventArgs e)
        {
            try { this.InitButtons(); } catch { }//初始化标准功能，因要应用于RibbonForm，所以加入异常处理
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        public override void DoAdd(IButtonInfo sender)
        {
            #region 按钮权限
            if (!BuildSqlHelper.IsExistList("B_IE_Operational_Definition", "LamexMESManager")) return;
            #endregion
            xtraTabPage1.PageEnabled = false;
            xtraTabPage2.PageEnabled = true;
            xtraTabPage2.PageVisible = true;

            IsEdit = "新增";
            xtraTabPage2.Tag = "新增";
            txtEditOperationName.Enabled = true;//新增时变为可编辑状态
            base.DoAdd(sender);   
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        public override void DoSave(IButtonInfo sender)
        {
            #region 按钮权限
            if (!BuildSqlHelper.IsExistList("B_IE_Operational_Definition", "LamexMESManager")) return;
            #endregion
            if (txtEditOperationName.Alter()) return;
            if (txtEditType.Alter()) return;
            if (IsEdit == "新增")
            {              
                B_IE_Operational_DefinitionInfo info = BaseFactory<B_IE_Operational_Definition>.Instance.GetEntriyList(" 1=1  and OPERATIONAL_NAME='" + txtEditOperationName.Text + "'").FirstOrDefault();
                if (info != null)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("动作名称已存在", "提示", MessageBoxButtons.OK);
                    return;
                }
                B_IE_Operational_DefinitionInfo model = new B_IE_Operational_DefinitionInfo();
                model.OPTION_VALUES = txtEditOptionValues.Text;
                model.WORKSTATION_LIST = txtEditWorkStation.Text;
                model.OPERATIONAL_NAME = txtEditOperationName.Text;
                model.TYPE = txtEditType.Text;
                model.UNIT = txtEditUnit.Text;
                model.UNIT_WORKHOURS = txtEditWorkHour.Text;
                model.ATTACH_WORKHOURS = txtEditAttachWorkHour.Text;
                model.DESCRIPTION = txtEditDescription.Text;
                model.EFFECTIVE_TS = DateTime.Now;//生效时间
                model.END_TS = Convert.ToDateTime("9999-12-31 23:59:59.000");//截止时间 新增为默认值
                model.CREATE_TS = DateTime.Now;//创建时间
                model.UPDATE_USER = Lamex.Common.Loginer.CurrentUser.Account;//修改人
                if (BaseFactory<B_IE_Operational_Definition>.Instance.Insert(model))
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("新增成功", "提示", MessageBoxButtons.OK);
                }
                else
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("新增失败", "提示", MessageBoxButtons.OK);
                }

            }
            if (IsEdit == "编辑")
            {
                B_IE_Operational_DefinitionInfo model = new B_IE_Operational_DefinitionInfo();
                model.IDNO =int.Parse(KeysName);
               // txtEditWorkStation.Enabled = false;//修改时变为不可编辑状态
                txtEditOperationName.Enabled = false;//修改时变为不可编辑状态
                //判断数据是否有修改，若没有变动，则提示出来
                if (strList[0] == txtEditOperationName.Text &&
                    strList[1] == txtEditType.Text && strList[2] == txtEditUnit.Text &&
                    strList[3] == txtEditWorkHour.Text && strList[4] == txtEditAttachWorkHour.Text &&
                    strList[5] == txtEditDescription.Text && strList[6] == txtEditOptionValues.Text&&
                    strList[7]==txtEditWorkStation.Text)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("没有修改任何一个记录", "提示", MessageBoxButtons.OK);
                    return;
                }
                //更新当前记录
                //如果日期为当天则只更新不新增,否则先更新后插入新纪录
                if (DateTime.Now.ToString("yyyy/MM/dd") == Convert.ToDateTime(strList[10]).ToString("yyyy/MM/dd"))//创建时间
                {
                    model.WORKSTATION_LIST = txtEditWorkStation.Text;
                    model.OPERATIONAL_NAME = txtEditOperationName.Text;
                    model.TYPE = txtEditType.Text;
                    model.UNIT = txtEditUnit.Text;
                    model.UNIT_WORKHOURS = txtEditWorkHour.Text;
                    model.ATTACH_WORKHOURS = txtEditAttachWorkHour.Text;
                    model.DESCRIPTION = txtEditDescription.Text;
                    model.OPTION_VALUES = txtEditOptionValues.Text;
                    model.EFFECTIVE_TS = Convert.ToDateTime(strList[8]);//有效时间
                    model.END_TS = Convert.ToDateTime(strList[9]);//截止时间
                    model.CREATE_TS = Convert.ToDateTime(strList[10]);//创建时间
                    model.UPDATE_USER = Lamex.Common.Loginer.CurrentUser.Account;//修改人
                    if (BaseFactory<B_IE_Operational_Definition>.Instance.Update(model))
                    {
                        DevExpress.XtraEditors.XtraMessageBox.Show("修改成功", "提示", MessageBoxButtons.OK);

                    }
                }
                else
                {
                    model.WORKSTATION_LIST = strList[7];
                    model.OPERATIONAL_NAME = strList[0];
                    model.TYPE = strList[1];
                    model.UNIT = strList[2];
                    model.UNIT_WORKHOURS = strList[3];
                    model.ATTACH_WORKHOURS = strList[4];
                    model.DESCRIPTION = strList[5];
                    model.OPTION_VALUES = strList[6];
                    model.EFFECTIVE_TS = Convert.ToDateTime(strList[8]);//有效时间
                    model.END_TS = DateTime.Now;//截止时间
                    model.CREATE_TS = Convert.ToDateTime(strList[10]);//创建时间
                    model.UPDATE_USER = strList[11];// Lamex.Common.Loginer.CurrentUser.Account;//修改人
                    if (BaseFactory<B_IE_Operational_Definition>.Instance.Update(model))
                    {
                        //更新成功后再进行插入操作
                        model.WORKSTATION_LIST = txtEditWorkStation.Text;
                        model.OPERATIONAL_NAME = txtEditOperationName.Text;
                        model.TYPE = txtEditType.Text;
                        model.UNIT = txtEditUnit.Text;
                        model.UNIT_WORKHOURS = txtEditWorkHour.Text;
                        model.ATTACH_WORKHOURS = txtEditAttachWorkHour.Text;
                        model.DESCRIPTION = txtEditDescription.Text;
                        model.OPTION_VALUES = txtEditOptionValues.Text;
                        model.EFFECTIVE_TS = DateTime.Now.AddDays(1);//生效时间=当前时间加1天
                        model.END_TS = Convert.ToDateTime("9999-12-31 23:59:59.000");//截止时间 新增为默认值
                        model.CREATE_TS = DateTime.Now;//创建时间
                        model.UPDATE_USER = Lamex.Common.Loginer.CurrentUser.Account;//修改人
                        if (BaseFactory<B_IE_Operational_Definition>.Instance.Insert(model))
                        {
                            DevExpress.XtraEditors.XtraMessageBox.Show("修改成功", "提示", MessageBoxButtons.OK);
                        }

                    }
                    else
                    {
                        DevExpress.XtraEditors.XtraMessageBox.Show("修改失败", "提示", MessageBoxButtons.OK);
                    }
                }
            }

            xtraTabPage2.PageEnabled = false;
            xtraTabPage1.PageEnabled = true;
            DoView(null);
            base.DoSave(sender);
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        public override void DoEdit(IButtonInfo sender)
        {
            #region 按钮权限
            if (!BuildSqlHelper.IsExistList("B_IE_Operational_Definition", "LamexMESManager")) return;
            #endregion
            if (gridView2.SelectedRowsCount == 0)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("【没有选中要删除要修改的数据！】", "提示", MessageBoxButtons.OK);
                return;
            }
            int m = gridView2.FocusedRowHandle;
            DataRow dr = gridView2.GetDataRow(m);
            KeysName = dr["IDNO"].ToString();
            //判断记录是否为最新的，若不是给出提示，避免修改出现错误
            string sql = @" select TOP 1 END_TS FROM [LamexMESManager].[dbo].[B_IE_Operational_Definition]
                                where  IDNO=" + KeysName + "";      
            DataTable dt = BaseFactory<B_IE_Operational_Definition>.Instance.GetDataTableBySql(sql);
            if (dt.Rows.Count > 0)
            {
                if (dt.Rows[0][0].ToString().Substring(0, 10) != "9999/12/31")
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("该工序的动作定义不是最新的记录，请查找最新记录修改！", "提示", MessageBoxButtons.OK);
                    return;
                }

            }
            xtraTabPage1.PageEnabled = false;
            xtraTabPage2.PageEnabled = true;
            xtraTabPage2.PageVisible = true;
            IsEdit = "编辑";
            xtraTabPage2.Tag = "编辑";
            txtEditOperationName.Enabled = false;//修改时变为不可编辑状态
            //***********************加载数据****************************//
          
            txtEditOperationName.Text = dr["OPERATIONAL_NAME"].ToString();
            txtEditType.Text = dr["TYPE"].ToString();
            txtEditUnit.Text = dr["UNIT"].ToString();
            txtEditWorkHour.Text = dr["UNIT_WORKHOURS"].ToString();
            txtEditAttachWorkHour.Text = dr["ATTACH_WORKHOURS"].ToString();
            txtEditDescription.Text = dr["DESCRIPTION"].ToString();
            txtEditOptionValues.Text = dr["OPTION_VALUES"].ToString();
            txtEditWorkStation.Text = dr["WORKSTATION_LIST"].ToString();
            //***********************赋值给数组****************************//
            for (int i = 0; i <= 11; i++)
            {
                strList[i] = dr[i + 1].ToString();
            }

            base.DoEdit(sender);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        public override void DoDelete(IButtonInfo sender)
        {
            #region 按钮权限
            if (!BuildSqlHelper.IsExistList("B_IE_Operational_Definition", "LamexMESManager")) return;
            #endregion
            if (gridView2.SelectedRowsCount == 0)
            {
                MessageBox.Show("【没有选中要删除的数据，请选择好数据！】");
                return;
            }
            int m = gridView2.FocusedRowHandle;
            DataRow dr = gridView2.GetDataRow(m);
            if (dr != null)
            {
                if (BaseFactory<B_IE_Operational_Definition>.Instance.Delete(dr["IDNO"].ToString()))
                {
                    MessageBox.Show("【数据删除成功！】");

                }
                else
                {
                    MessageBox.Show("【数据删除失败！】");

                }
            }
            DoView(null);
            base.DoDelete(sender);
        }
        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="sender"></param>
        public override void DoExport(IButtonInfo sender)
        {

            base.DoExport(sender);
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        public override void DoView(IButtonInfo sender)
        {
            string sqlWhere = "";
            string sql = " select * from B_IE_Operational_Definition where 1=1 ";
            if (!string.IsNullOrEmpty(txtOperationDefine.Text))
            {
                sqlWhere += " and OPERATIONAL_NAME like '%" + txtOperationDefine.Text + "%'";
            }
            //if (!string.IsNullOrEmpty(txtType.Text))
            //{
            //    sqlWhere += " and TYPE like '%" + txtType.Text + "%'";
            //}
            if (!string.IsNullOrEmpty(txtWorkStation.Text))
            {
                sqlWhere += " and WORKSTATION_LIST like '%" + txtWorkStation.Text + "%'";
            }

            sql = sql + sqlWhere;
           // DataTable dt = BaseBuildSql.GetDataTableBySql(sql);
            DataTable dt = BaseFactory<B_IE_Operational_Definition>.Instance.GetDataTableBySql(sql);

            DoBindingSummaryGrid(dt);
            base.DoView(sender);
        }
        public override void DoCancel(IButtonInfo sender)
        {

            base.DoCancel(sender);
        }
        public override void DoReturn(IButtonInfo sender)
        {
            xtraTabPage1.PageEnabled = true;
            xtraTabPage2.PageEnabled = false;
            IsEdit = "";
            base.DoReturn(sender);
        }
        public override void DoImport(IButtonInfo sender)
        {
            #region 按钮权限
            if (!BuildSqlHelper.IsExistList("B_IE_Operational_Definition", "LamexMESManager")) return;
            #endregion
            FrmImport frm = new FrmImport("B_IE_Operational_Definition");
            frm.ShowDialog();
            base.DoImport(sender);
        }
        public void Clear()
        {
            txtEditOperationName.Text = "";
            //txtEditType.Text ="";
            //txtEditUnit.Text = "";
            //txtEditDescription.Text = "";
        }
    }

}